## ******************************
##    C3 Control for Labor Strikes       
##       Hao Zhang & Ye Zhang
##            2025/7/24
## ******************************

# load packages
library(readstata13)
library(lfe)
library(stargazer)
library(tidyverse)
library(doBy)
library(data.table)
library(DescTools)
library(haven)
library(readxl)

# set working directory
setwd("../raw data")

# read in data
strike <- read_excel("strikemap 20210717-updated.xlsx", col_types = "text")

# read in citycode
citylist <- read_excel("citylist encoding.xlsx")
citylist <- citylist %>% rename(City = cityname) %>% select(City, citycode) %>% unique() 

# get rid of duplicate codes but do not affect our final estimation
citylist <- citylist %>% filter(citycode != 3212) %>% filter(citycode != 4509)  %>% filter(citycode != 2307)

# join with strike
strike <- left_join(strike, citylist, by = "City")
strike$count <- 1
strike$year <- as.numeric(substr(strike$Date, 0, 4))
strike <- strike %>% mutate(year = year + 1) %>% mutate(year = as.character(year))
strike <- summaryBy(count ~ citycode + year, FUN = sum, data = strike)
strike <- strike %>% rename(city4 = citycode)
strike$year <- as.numeric(strike$year)

# join with CFPS data
load("../analysis data/individual analysis.RData")
data <- left_join(data, strike, by = c("city4", "year"))
data$count.sum[is.na(data$count.sum) == TRUE] <- 0

# run inverse hyperbolic sine
ihs <- function(x) {
  y <- log(x + sqrt(x ^ 2 + 1))
  return(y)
}

# run regressions
m1 <- felm(unemployment ~ gdp_pressure + ihs(gdp_growth) + log(gdppc)  + ihs(deficit/gdp) + ihs(tax_absc_firm/gdp) + log(FDI + 1) + ihs(count.sum) + log(unemp/pop/10000) | 
             city4 + year + pid + begin + educ.y + ethnicity + gender | 0 | city4, data = data)

m2 <- felm(medical ~ gdp_pressure + ihs(gdp_growth) + log(gdppc)  + ihs(deficit/gdp) + ihs(tax_absc_firm/gdp) + log(FDI + 1) + ihs(count.sum) + log(hosp/pop/10000) | 
             city4 + year + pid + begin + educ.y + ethnicity + gender | 0 | city4, data = data)

m3 <- felm(pension ~ gdp_pressure + ihs(gdp_growth) + log(gdppc)  + ihs(deficit/gdp) + ihs(tax_absc_firm/gdp) + log(FDI + 1) + ihs(count.sum) + log(hosp/pop/10000) | 
             city4 + year + pid + begin + educ.y + ethnicity + gender | 0 | city4, data = data)

stargazer(m1, m2, m3, title = "Interjurisdictional Competition and Social Insurances (Labor Strikes)", 
          dep.var.labels = c("Social Insurance Participation"),
          omit.stat = c("rsq", "adj.rsq", "ser"), 
          font.size = "small", 
          add.lines = list(c("Mayor Characteristics", "Y", "Y", "Y"),
                           c("Individual Fixed Effects", "Y", "Y", "Y"),
                           c("City Fixed Effects", "Y", "Y", "Y"), 
                           c("Year Fixed Effects", "Y", "Y", "Y")))
